package main

import (
	"fmt"
	"os"
	"peilian-api/app/global/variable"
	_ "peilian-api/bootstrap"
	"peilian-api/cronjob"
	"peilian-api/routers"

	"golang.org/x/sync/errgroup"
)

var (
	g errgroup.Group
)

func routerService() error {
	router := routers.InitApiRouter()
	return router.Run(variable.Config.App.Port)
}

func cronService() error {
	cronjob.InitCron()
	cronjob.PermanentTask()
	fmt.Printf("cronService run")
	return nil
}

func main() {
	g.Go(func() error {
		return routerService()
	})
	cronFiring := os.Getenv("CRON_FIRING")
	if cronFiring == "start" { // 本地不启动定时任务和常驻进程
		g.Go(func() error {
			return cronService()
		})
	}
	if err := g.Wait(); err != nil {
		fmt.Println(err)
	}
}
